package com.example.assisteddriving.util;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.assisteddriving.bean.Drivelog_;
import com.example.assisteddriving.bean.Xinlvlog;
import com.example.assisteddriving.dao.DrivelogDao;
import com.example.assisteddriving.service.AnalyseSevice;

import java.util.ArrayList;
import java.util.List;

public class Avg {
    public static final String PREFERENCE_NAME = "log";  //文件名
    public static int MODE = MODE_PRIVATE;  //操作模式
    public void avgtime(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, MODE);
        DrivelogDao drivelogDao=new DrivelogDao(context);
        List<Drivelog_> list=new ArrayList<>();
        int n=sharedPreferences.getInt("log",0);
        long time0 = 0,tiredtime1,averagetiredtime = 0,sleeptime1,averagesleeptime = 0;
        long t=0,s=0;

        if(n>0){
            for (int i=1;i<=n;i++){
                list= drivelogDao.getlog(String.valueOf(i));
                if(list!=null){
                    for (int j=0;j<list.size();j++){
                        if (list.get(j).getTired().equals("0")&&list.get(j).getSleep().equals("0")){
                            time0=list.get(j).getTime();
                        }
                        if (list.get(j).getTired().equals("1")){
                            tiredtime1=list.get(j).getTime();
                            averagetiredtime=tiredtime1-time0;
                            t++;
                        }
                        if (list.get(j).getSleep().equals("1")){
                            sleeptime1=list.get(j).getTime();
                            averagesleeptime=averagesleeptime+sleeptime1-time0;
                            s++;
                        }

                    }
                }
            }
            averagetiredtime=averagetiredtime/t;
            averagesleeptime=averagesleeptime/s;

            Log.v("asd", String.valueOf(averagesleeptime));
            Log.v("asd", String.valueOf(averagetiredtime));
        }
    }

    private void avgxl(Context context) {
        List<Xinlvlog> list=new ArrayList<>();
        DrivelogDao drivelogDao=new DrivelogDao(context);
        list=drivelogDao.getxl();
        float xl,tw,xy;
        int A=0,B=0,C=0;
        if (list.size()!=0) {
            for (int i=0;i<list.size();i++){
                float a= Float.parseFloat(list.get(i).getXinlv())*100;
                float b=Float.parseFloat(list.get(i).getTiwen())*100;
                float c=Float.parseFloat(list.get(i).getXueyang())*100;
                A=(int)a+A;
                B=(int)b+B;
                C=(int)c+C;
                xl=A/100;
            }
            A=(int) A/list.size();
            B=(int) B/list.size();
            C=(int) C/list.size();
            xl=A/100;
            tw=B/100;
            xy=C/100;
        }
    }
}
